/**
 * 菜单功能定义管理主界面
 * @author Linzongxue 2011-10-07
 */
define(function(){
	var MenuMainPanel = Ext.extend(Ext.ux.tree.TreeGrid, {
		height: 580,
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    enableSort: false,
        animate: true,
		initComponent: function(){
			Ext.apply(this, {
				tbar: [
	            	{text: '添加目录', handler: this.addMenuFolder, scope: this, iconCls: 'icon-add'},
	            	{text: '添加菜单', handler: this.addMenu, scope: this, iconCls: 'icon-add'},
	            	{text: '修改', handler: this.modifyMenu,scope: this, iconCls: 'icon-edit'},
	            	{text: '删除', handler: this.removeMenu,scope: this, iconCls: 'icon-delete'},
					'-',
					{text: '刷新', handler: this.refresh, scope: this, iconCls: 'icon-refresh'}
	            ],
				root: new Ext.tree.AsyncTreeNode({
					id:'root',
					listeners:{
			        	expand: function(node){ //默认展开到第二级
			        		node.eachChild(function(n){n.expand();});
		        		}
		        	}
				}),
		        rootVisible: false,
				dataUrl: 'sys/menu/getTreeGridData.do',
				columns: [
	                 {header: '标题', dataIndex: 'text', width: 200},
	                 {header: '名称', dataIndex: 'name', width: 120},
	                 {header: '目标值', dataIndex: 'value', width: 200},
	                 {header: '受权按钮', dataIndex: 'buttons', width: 200, renderer: this.rendererButtons},
	                 {header: '排序号', dataIndex: 'sortNum', width: 50}
	            ]
			});
			MenuMainPanel.superclass.initComponent.call(this);
			this.on('dblclick',  function(node, e){
        		if (this.getSelectionModel().getSelectedNode()) this.modifyMenu();
        	});
		},
		addMenuFolder: function(){
			var parentNode = this.getSelectionModel().getSelectedNode();
			use('sys.menu.MenuFolderWindow', function(Win){
				var win = new Win({
					parentMenu: parentNode || this.getRootNode(),
					callback: this.saveCallback,
					cbScope: this
				});
				win.show();
			}, this);
		},
		addMenu: function(){
			var parentNode = this.getSelectionModel().getSelectedNode();
			if (parentNode && parentNode.isLeaf()) parentNode = parentNode.parentNode;
			use('sys.menu.MenuWindow', function(Win){
				var win = new Win({
					parentMenu: parentNode || this.getRootNode(),
					callback: this.saveCallback,
					cbScope: this
				});
				win.show();
			}, this);
		},
		modifyMenu: function(){
			var node = this.getSelectionModel().getSelectedNode();
			if (!node){
				cu.hint('请选择要修改的菜单');
				return;
			}
			var module = (node.attributes.type == 'FOLDER' ? 'sys.menu.MenuFolderWindow' : 'sys.menu.MenuWindow');
			use(module, function(Win){
				var win = new Win({
					modifiedId: node.attributes.id,
					parentMenu: node.parentNode,
					callback: function(parentNodePath, menu){
						if (node.parentNode.getPath() != parentNodePath){ //修改了父节点，原来的节点要删除
							node.remove();
						}
						//刷新父节点，并定位被修改的节点
						this.saveCallback(parentNodePath, menu);
					},
					cbScope: this
				});
				win.show();
			}, this);
		},
		removeMenu: function(){
			var nodes = this.getChecked();
			if (!nodes || nodes.length == 0){
				cu.hint('请选择要删除的菜单');
				return;
			}
			cu.confirm('确定要删除选中的菜单吗？', function(){
				var ids = lu.pluck(nodes, 'attributes.id');
				cu.post({
					url: 'sys/menu/delete.do', 
					params: {ids: lu.pluck(nodes, 'attributes.id')}, 
					success: function(){
						Ext.each(nodes, function(n){
							n.ownerTree && n.remove();
						});
						au.getMenuPanel().refresh();
					}, 
					scope: this
				});
			}, this);
		},
		//添加和修改菜单后的回调方法，刷新并定位保存后的菜单
		saveCallback: function(parentNodePath, menu){
			this.selectPath(parentNodePath, 'id', function(success, node){
				var tree = node.getOwnerTree();
				tree.getLoader().load(node, function(){
					tree.selectPath(parentNodePath + '/' + menu.id);
				});
			});
			au.getMenuPanel().refresh();
		},
		//刷新，重新加载菜单数据
		refresh: function(){
			this.getLoader().load(this.root, function(node){node.fireEvent('expand', node);});
			au.getMenuPanel().refresh(); //刷新系统菜单
		},
		rendererButtons: function(v){
			if (!v) return '';
			var text = v.join('&nbsp;|&nbsp;');
			return '<span title="' + text + '">' + text + '</span>';
		}
	});
	return MenuMainPanel;
});